//
//  YGWorldNewsViewModel.m
//  luGongPro
//
//  Created by 王庆学 on 2017/8/24.
//  Copyright © 2017年 王庆学. All rights reserved.
//

#import "YGWorldNewsViewModel.h"
#import "EFSImportantInformationCollection.h"
#import "YSServiceManager.h"
#import "YSUserManager.h"
#import "TSMessage+YTX.h"
#import "YGRefreshModel.h"
#import "YYCache+NetworkCache.h"

@interface YGWorldNewsViewModel ()

@property (nonatomic, strong) NSMutableArray *models;

@end

@implementation YGWorldNewsViewModel

- (RACSignal *)refreshNewsList{
    RACReplaySubject *subject = [RACReplaySubject subject];
    YSRequest *request = [YSRequest requestWithName:@"aggregate.list"];
    request.requestSerializerType = YSRequestSerializerTypeJSON;
    
    @weakify(self);
    NSDictionary *params = @{@"serverId":[YSServiceManager serviceId],
                             @"token":objectOrEmptyStr([YSUserManager sharedManager].currentUser.token)
                             };
    [request sendWithParameters:params success:^(NSArray * response) {
        @strongify(self);
        if (response.count){
            NSArray *items = [MTLJSONAdapter modelsOfClass:[EFSImportantInformationModel class] fromJSONArray:response error:NULL];
            self.models = [items mutableCopy];
            [[YYCache sharedCache] setObject:items forKey:request.requestUrl];
            [subject sendNext:items];
            [subject sendCompleted];
        }else{
            NSArray *cacheVms = (NSArray *)[[YYCache sharedCache]objectForKey:request.requestUrl];
            [subject sendNext:cacheVms];
            [subject sendCompleted];
        }
    } failure:^(YSRequest * _Nonnull request) {
        NSArray *chcheVms = (NSArray *)[[YYCache sharedCache]objectForKey:request.requestUrl];
        [subject sendNext:chcheVms];
        [subject sendCompleted];
    }];
    return subject;
}

- (RACSignal *)fetchMoreNews{
    RACReplaySubject *subject = [RACReplaySubject subject];
    YSRequest *request = [YSRequest requestWithName:@"aggregate.list"];
    request.requestSerializerType = YSRequestSerializerTypeJSON;
    
    @weakify(self);
    NSDictionary *params = [self importantInformationParams:YES];
    [request sendWithParameters:params success:^(NSArray * response) {
        @strongify(self);
        if (response.count){
            NSArray *items = [MTLJSONAdapter modelsOfClass:[EFSImportantInformationModel class] fromJSONArray:response error:NULL];
            [self.models addObjectsFromArray:items];
            [subject sendNext:self.models];
            [subject sendCompleted];
        }else{
            [subject sendNext:@[]];
            [subject sendCompleted];
        }
    } failure:^(YSRequest * _Nonnull request) {
        [subject sendError:[NSError new]];
    }];
    return subject;
}

- (NSDictionary *)importantInformationParams:(BOOL) loadMore
{
    EFSImportantInformationModel *lastModel = [self.models lastObject];
    NSNumber *timestamp = lastModel.publishTimeMs;
    if (!timestamp || !loadMore){
        timestamp = [NSNumber numberWithLong:(long)([[NSDate date] timeIntervalSince1970]*1000)];
    }
    return @{@"serverId":[YSServiceManager serviceId],
             @"token":objectOrEmptyStr([YSUserManager sharedManager].currentUser.token),
             @"time":timestamp};
}

- (void)clickCellOnModel:(EFSImportantInformationModel *)model{
    NSDictionary *itemDict = [MTLJSONAdapter JSONDictionaryFromModel:model];
    NSMutableDictionary *dicM = [itemDict mutableCopy];
    dicM[@"viewTitle"] = @"钱币咨询";
    if ([itemDict[@"messageType"] longValue] == MessageTypeShotMessage){
        return;
    }else if ([itemDict[@"messagetType"] longValue] == MessageTypeActivity){
        [EFSJump jump:JumpTypeActivity withData:dicM from:[UIViewController visibleViewController]];
    }else{
        [EFSJump jump:JumpTypeActivity withData:dicM from:[UIViewController visibleViewController]];
    }
}

@end
